package com.collect.senegal.zhiScene.zhiWidget.zhiPhonebookView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.collect.senegal.MyApplication;
import com.collect.senegal.R;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRPhoneBookEntity;
import com.collect.senegal.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;

import java.util.ArrayList;

public class ZHIPhoneBookView extends PopupWindow {

    private View mMenuView;
    private LinearLayout ll_pop_layout;
    private RecyclerView rc_list;
    private ZHINextStepView btn_close_phonebook;
    private ImageView iv_close;
    private String mType;

    private ArrayList<ZHIRPhoneBookEntity> mPhoneBookEntities;
    ZHIPhoneBookAdjuster mZHIPhoneBookAdjuster = new ZHIPhoneBookAdjuster();

    private OnPhoneBookClickListener mOnPhoneBookClickListener;

    @SuppressLint({"ClickableViewAccessibility", "CutPasteId"})
    public ZHIPhoneBookView(Context context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popview_phonebook, null);
        ll_pop_layout = mMenuView.findViewById(R.id.ll_pop_layout);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopWindowFromBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x30000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setClippingEnabled(false);
        rc_list = mMenuView.findViewById(R.id.rc_list);
        iv_close = mMenuView.findViewById(R.id.iv_close);
        btn_close_phonebook = mMenuView.findViewById(R.id.btn_close_phonebook);

        iv_close.setOnClickListener(v -> dismiss());
        btn_close_phonebook.setOnMenuItemDidChangedListener(item -> dismiss());

        mMenuView.setOnTouchListener((v, event) -> {
            int height = mMenuView.findViewById(R.id.ll_pop_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });

        rc_list.setLayoutManager(new GridLayoutManager(context, 1));
        rc_list.setAdapter(mZHIPhoneBookAdjuster);
        mZHIPhoneBookAdjuster.setPhoneBookItemClickListener((ftrOrderEntity, type) -> {
            if (type.equalsIgnoreCase(ZHIPhoneBookAdjuster.ITEM_TYPE_CALL_PHONE)) {
                if (mOnPhoneBookClickListener != null) {
                    mOnPhoneBookClickListener.onCallPhoneClick(ftrOrderEntity);
                }
            } else if (type.equalsIgnoreCase(ZHIPhoneBookAdjuster.ITEM_TYPE_RECORD_TAG)) {
                if (mOnPhoneBookClickListener != null) {
                    mOnPhoneBookClickListener.onRecTagClick(ftrOrderEntity);
                }
            } else if (type.equalsIgnoreCase(ZHIPhoneBookAdjuster.ITEM_TYPE_RECORD_WHATSAPP)) {
                if (mOnPhoneBookClickListener != null) {
                    mOnPhoneBookClickListener.onWhatsappClick(ftrOrderEntity);
                }
            }
        });
    }

    public void setPhoneBookTitle(String title) {
        TextView tv_option_title = (TextView) mMenuView.findViewById(R.id.tv_pop_title);
        tv_option_title.setText(title);
    }

    public void setNewData(ArrayList<ZHIRPhoneBookEntity> phoneBookEntities) {
        try {
            setPhoneBookTitle(MyApplication.getAppContext().getString(R.string.address_book) + "(" + phoneBookEntities.size() + ")");
            mPhoneBookEntities = phoneBookEntities;
            mZHIPhoneBookAdjuster.setNewData(phoneBookEntities);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOptionType(String type) {
        this.mType = type;
    }

    public void setPhoneBookClickListener(OnPhoneBookClickListener phoneBookClickListener) {
        this.mOnPhoneBookClickListener = phoneBookClickListener;
    }

    public interface OnPhoneBookClickListener {
        void onRecTagClick(ZHIRPhoneBookEntity phoneBookEntity);

        void onCallPhoneClick(ZHIRPhoneBookEntity phoneBookEntity);

        void onWhatsappClick(ZHIRPhoneBookEntity phoneBookEntity);
    }

}